/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "controls.hpp"
#include "glutils.hpp"

/*********** Button **************/

CGLButton::CGLButton()
:buttonCallback(NULL),
x(0),y(0),z(0),
pressed(false),
focused(false),
enabled(true)
{
  CGLPickable::SetCallback(this);
}

void CGLButton::SetPosition(const float x, const float y, const float z)
{
  this->x=x;
  this->y=y;
  this->z=z;
}

void CGLButton::GetPosition(float &x, float &y, float &z)
{
  x=this->x;
  y=this->y;
  z=this->z;
}

void CGLButton::SetCallback(CButtonCallback *callback)
{
  this->buttonCallback=callback;
}

void CGLButton::Enable(bool enable)
{
  this->enabled=enable;
  EnablePicking(enable);
}

void CGLButton::Draw()
{
  int mode;
  glGetIntegerv(GL_RENDER_MODE,&mode);
  glPushMatrix();
  if (mode==GL_SELECT)
  {
    glTranslatef(x,y,z);
    DrawPickable();
  } else {
    glTranslatef(x,y,z);
    if (enabled)
    {
      if (!pressed)
      {
        DrawUp();
      } else if (pressed) {
        DrawDown();
      }
      if (focused)
      {
        DrawFocused();
      }
    } else {
      DrawDisabled();
    }
  }
  glPopMatrix();
}

void CGLButton::OnFocus(TGLPickEvent event)
{
  focused=true;
}

void CGLButton::OnFocusLost(TGLPickEvent event)
{
  focused=false;
}

void CGLButton::OnPick(TGLPickEvent event)
{
  pressed=true;
}

void CGLButton::OnRelease(TGLPickEvent event)
{
  pressed=false;
  if (buttonCallback)
    buttonCallback->OnButtonEvent(this);
}


/********* 2d text button ************/

C2DTextButton::C2DTextButton()
:width(40),height(20),
text("Button")
{
  
}

void C2DTextButton::SetText(std::string text)
{
  this->text=text;
  this->width=getTextWidth(GLUT_BITMAP_HELVETICA_12,text.c_str())+8;
}

void C2DTextButton::DrawPickable()
{
  glRectf(0,0,width,height);
}

void C2DTextButton::DrawUp()
{
  glColor3f(0,0.1,0.8);
  glRectf(0,0,width,height);
  
  glColor3f(0.9,0.9,0.9);
  drawtext();
  
  if (focused) {
    glColor3f(0.8,0.9,1);
  } else {
    glColor3f(0.4,0.5,0.9);
  }
  
  glLineWidth(2);
  glBegin(GL_LINE_LOOP);
  glVertex2f(0,0);
  glVertex2f(0,height);
  glVertex2f(width,height);
  glVertex2f(width,0);
  glEnd();
  
}

void C2DTextButton::DrawDown()
{
  glColor3f(0,0.1,0.4);
  glRectf(0,0,width,height);
  
  glColor3f(0.7,0.7,0.7);
  drawtext();
  
  if (focused) {
    glColor3f(0.6,0.7,0.8);
  } else {
    glColor3f(0.4,0.5,0.6);
  }
  
  glLineWidth(2);
  glBegin(GL_LINE_LOOP);
  glVertex2f(0,0);
  glVertex2f(0,height);
  glVertex2f(width,height);
  glVertex2f(width,0);
  glEnd();
  
}

void C2DTextButton::DrawFocused()
{
  /*
  glColor3f(0,0.3,1);
  glLineWidth(2);
  glBegin(GL_LINE_LOOP);
  glVertex2f(0,0);
  glVertex2f(0,height);
  glVertex2f(width,height);
  glVertex2f(width,0);
  glEnd();
  */
}

void C2DTextButton::DrawDisabled()
{
  glColor3f(0.5,0.5,0.7);
  glRectf(2,2,width-2,height-2);
  
  glColor3f(0.3,0.3,0.3);
  drawtext();
  
  glColor3f(0.6,0.6,0.8);
  
  glLineWidth(2);
  glBegin(GL_LINE_LOOP);
  glVertex2f(2,2);
  glVertex2f(2,height-2);
  glVertex2f(width-2,height-2);
  glVertex2f(width-2,2);
  glEnd();
}

void C2DTextButton::drawtext()
{
  drawText(4,6,0,GLUT_BITMAP_HELVETICA_12,text.c_str());
}

